﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tesla.Service.WcfServiceCommon;
using System.ServiceModel;
using Tesla.Service.ServiceContracts;
using Tesla.Service.DataContracts;
using Tesla.Util.ValidateEntity;


namespace Tesla.Web
{
    public class ProjectServiceDispatcher
    {
        public IList<BaseDC> DisplayAllProject(int gridSize, ref int? totalRecordCount)
        {

            ISvcProject _proxySvcProject;
            ChannelFactory<ISvcProject> chfProject = null; IList<BaseDC> Projects = null;

            try
            {
                chfProject = GADNChannelFactoryProvider.CreateChannelFactory<ISvcProject>();
                _proxySvcProject = chfProject.CreateChannel();
                Projects = _proxySvcProject.DisplayAll(gridSize, ref totalRecordCount);
            }
            catch (FaultException gfx)
            {
                if (chfProject.State == CommunicationState.Faulted || chfProject.State == CommunicationState.Opened)
                    chfProject.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfProject.State == CommunicationState.Faulted || chfProject.State == CommunicationState.Opened)
                    chfProject.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfProject.Close();
            }
            return Projects;
        }



        public IList<BaseDC> SearchProjectByName(string name, int gridSize, ref int? totalRecordCount)
        {
            ISvcProject _proxySvcProject;
            ChannelFactory<ISvcProject> chfProject = null; IList<BaseDC> Projects = null;

            try
            {
                chfProject = GADNChannelFactoryProvider.CreateChannelFactory<ISvcProject>();
                _proxySvcProject = chfProject.CreateChannel();
                Projects = _proxySvcProject.SearchByName(name, gridSize, ref totalRecordCount);
            }
            catch (FaultException gfx)
            {
                if (chfProject.State == CommunicationState.Faulted || chfProject.State == CommunicationState.Opened)
                    chfProject.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfProject.State == CommunicationState.Faulted || chfProject.State == CommunicationState.Opened)
                    chfProject.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfProject.Close();
            }
            return Projects;
        }

        public IList<BaseDC> ReadProject(int id)
        {
            ISvcProject _proxySvcProject;
            ChannelFactory<ISvcProject> chfProject = null; IList<BaseDC> Projects = null;

            try
            {
                chfProject = GADNChannelFactoryProvider.CreateChannelFactory<ISvcProject>();
                _proxySvcProject = chfProject.CreateChannel();
                Projects = _proxySvcProject.Read(id);
            }
            catch (FaultException gfx)
            {
                if (chfProject.State == CommunicationState.Faulted || chfProject.State == CommunicationState.Opened)
                    chfProject.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfProject.State == CommunicationState.Faulted || chfProject.State == CommunicationState.Opened)
                    chfProject.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfProject.Close();
            }
            return Projects;
        }

        public IList<BaseDC> ReadProjectByRowNumberRange(int startRecordNo, int endRecordNo, string sortColName, string sortDirection, string searchString)
        {
            ISvcProject _proxySvcProject;
            ChannelFactory<ISvcProject> chfProject = null; IList<BaseDC> Projects = null;

            try
            {
                chfProject = GADNChannelFactoryProvider.CreateChannelFactory<ISvcProject>();
                _proxySvcProject = chfProject.CreateChannel();
                Projects = _proxySvcProject.ReadByRange(startRecordNo, endRecordNo, sortColName, sortDirection, searchString);
            }
            catch (FaultException gfx)
            {
                if (chfProject.State == CommunicationState.Faulted || chfProject.State == CommunicationState.Opened)
                    chfProject.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfProject.State == CommunicationState.Faulted || chfProject.State == CommunicationState.Opened)
                    chfProject.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfProject.Close();
            }
            return Projects;
        }


        public int? CreateProject(BaseDC bdc)
        {
            ISvcProject _proxySvcProject;
            ChannelFactory<ISvcProject> chfProject = null; int? success = 0;

            try
            {
                chfProject = GADNChannelFactoryProvider.CreateChannelFactory<ISvcProject>();
                _proxySvcProject = chfProject.CreateChannel();
                success = _proxySvcProject.Create(bdc);
            }
            catch (FaultException gfx)
            {
                if (chfProject.State == CommunicationState.Faulted || chfProject.State == CommunicationState.Opened)
                    chfProject.Abort();
                throw gfx;
            }
            catch (TimeoutException tx)
            {
                if (chfProject.State == CommunicationState.Faulted || chfProject.State == CommunicationState.Opened)
                    chfProject.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfProject.Close();
            }
            return success;
        }

        public int UpdateProject(BaseDC bdc)
        {
            ISvcProject _proxySvcProject;
            ChannelFactory<ISvcProject> chfProject = null; int updateResult = 0;

            try
            {
                chfProject = GADNChannelFactoryProvider.CreateChannelFactory<ISvcProject>();
                _proxySvcProject = chfProject.CreateChannel();
                updateResult = _proxySvcProject.Update(bdc);
            }
            catch (FaultException gfx)
            {
                if (chfProject.State == CommunicationState.Faulted || chfProject.State == CommunicationState.Opened)
                    chfProject.Abort();
                throw gfx;
            }
            catch (TimeoutException tx)
            {
                if (chfProject.State == CommunicationState.Faulted || chfProject.State == CommunicationState.Opened)
                    chfProject.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfProject.Close();
            }
            return updateResult;
        }
    }
}

